package o2o.web.actions.notice;


//发送通知消息

import com.jinyou.utils.common.ValidateUtil;
import jPush.dao.JPushMessage;
import notice.buzz.NoticeBusiness;
import notice.dao.NoticeRange;
import notice.dao.NoticeType;
import notice.dao.NoticeVO;
import o2o.cache.user.UserInfoMemory;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.user.UserInfo;
import o2o.task.jPush.JPushTask;
import o2o.task.notice.NoticeTask;

import java.util.HashSet;
import java.util.Set;

public class Notice {


    /**
     * 批量发送系统消息
     *
     * @param usernameSet
     * @param userType
     * @param title
     * @param content
     */
    public static void publishSystemNotice(Set<String> usernameSet, Integer userType, String title, String content) {
        if (usernameSet != null && usernameSet.size() > 0) {
            //发送一条订单通知
            NoticeVO ntcvo = new NoticeVO();
            ntcvo.setType(NoticeType.system);//消息类型：系统通知
            ntcvo.setTitle(title);
            ntcvo.setContent(content);
            String singStr = NoticeRange.all;
            if (userType != null) {
                if (UserType.POSTMAN - userType == 0) {
                    singStr = NoticeRange.postman;
                }
                if (UserType.USER - userType == 0) {
                    singStr = NoticeRange.user;
                }
                if (UserType.SHOP_OWNER - userType == 0) {
                    singStr = NoticeRange.shop;
                }
            }
            ntcvo.setRange(NoticeRange.getRangeName(singStr));
            NoticeBusiness.saveNotice(NoticeAction.getOrgIdByToRange(singStr), "", usernameSet, ntcvo);
        }
    }

    //系统发送通知给某人或某些人
    public static void publishOrderNotice(String toRange, String title, String content) {
        NoticeVO ntcvo = new NoticeVO();
        ntcvo.setType(NoticeType.order);//消息类型：系统通知
        ntcvo.setTitle(title);
        ntcvo.setContent(content);

        ntcvo.setRange(NoticeRange.getRangeName(NoticeRange.single));//指定人
        Long orgId = NoticeAction.getOrgIdByToRange(NoticeRange.single);

        if (ValidateUtil.isNotNull(toRange)) {
            String[] toUsers = toRange.split(",");
            if (null != toUsers && toUsers.length > 0) {
                for (int i = 0; i < toUsers.length; i++) {
                    if (ValidateUtil.isNotNull(toUsers[i])) {
                        UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(toUsers[i]);
                        if (null != userInfo) {
                            HashSet<String> userSet = new HashSet<>();
                            userSet.add(userInfo.getUsername());
                            NoticeBusiness.saveNotice(NoticeAction.getOrgIdByToRange(NoticeRange.single), "", userSet, ntcvo);
                        }
                    }
                }
            }
        }
    }

    //发送推送
    public static void publishSystemNotice(String username, String title, String content) {
        if (ValidateUtil.isNotNull(username)) {
            //发送一条订单通知
            NoticeVO ntcvo = new NoticeVO();
            ntcvo.setType(NoticeType.system);//消息类型：系统通知
            ntcvo.setTitle(title);
            ntcvo.setContent(content);

            ntcvo.setRange(NoticeRange.getRangeName(NoticeRange.single));//指定人
            if (ValidateUtil.isNotNull(username)) {
                HashSet<String> userSet = new HashSet<>();
                userSet.add(username);
                NoticeBusiness.saveNotice(NoticeAction.getOrgIdByToRange(NoticeRange.single), "", userSet, ntcvo);
            }
        }
    }

    //----------------------------------推送通知
    //发送推送
    public static void publishNotice2Person(String username,
                                            String title, String content) {
        publishNotice2Person(null, username, JPushMessage.USER, "",
                NoticeType.system,
                title, content, "");
    }

    public static void publishNotice2Person(String sysAppKey, String username, Integer jpUserType,
                                            Integer noticeType,
                                            String title, String content) {

        publishNotice2Person(sysAppKey, username, jpUserType, "",
                noticeType,
                title, content, "");
    }


//    Notice.publishNotice2Person(sysAppKey, orderInfo.getUsername(), JPushMessage.USER, NoticeType.order, "订单信息", "您的订单（订单编号：" + orderNo + "），商家已接单");

    //【服务通知】商家接单, 骑手接单, 订单已送达状态
    public static void publishNotice2Person(String sysAppKey, String username, Integer jpUserType,
                                            Integer noticeType,
                                            String title, String content, String orderNo) {

        publishNotice2Person(sysAppKey, username, jpUserType, "",
                noticeType,
                title, content, orderNo);
    }


    public static void publishNotice2Person(String sysAppKey, String username, Integer jpUserType, String jpSubType,
                                            Integer noticeType,
                                            String title, String content, String orderNo) {
        if (ValidateUtil.isNull(username)) {
            return;
        }
        //发送一条订单通知
        NoticeVO ntcvo = new NoticeVO();
//            ntcvo.setType(NoticeType.system);//消息类型：系统通知
        ntcvo.setType(noticeType);//消息类型
        ntcvo.setTitle(title);
        ntcvo.setContent(content);

        ntcvo.setRange(NoticeRange.getRangeName(NoticeRange.single));//指定人
        if (ValidateUtil.isNotNull(username)) {
            HashSet<String> userSet = new HashSet<>();
            userSet.add(username);
            NoticeBusiness.saveNotice(NoticeAction.getOrgIdByToRange(NoticeRange.single), "", userSet, ntcvo);
        }


        JPushMessage jPushMessage = new JPushMessage(false, null, title, content);
        jPushMessage.setUsername(username);
        jPushMessage.setPushUserType(jpUserType);
        jPushMessage.setJpSubType(jpSubType);
        jPushMessage.setOrderNo(orderNo);
        JPushTask.jPushMessageList.add(jPushMessage);
        NoticeTask.messageList.add(jPushMessage);// 公众号通知、服务通知

//        if (ValidateUtil.isNotNull(username)) {
//            //发送一条订单通知
//            NoticeVO ntcvo = new NoticeVO();
////            ntcvo.setType(NoticeType.system);//消息类型：系统通知
//            ntcvo.setType(noticeType);//消息类型
//            ntcvo.setTitle(title);
//            ntcvo.setContent(content);
//
//            ntcvo.setRange(NoticeRange.getRangeName(NoticeRange.single));//指定人
//            if (ValidateUtil.isNotNull(username)) {
//                HashSet<String> userSet = new HashSet<>();
//                userSet.add(username);
//                NoticeBusiness.saveNotice(NoticeAction.getOrgIdByToRange(NoticeRange.single), "", userSet, ntcvo);
//            }
//
//
//
//            JPushMessage jPushMessage = new JPushMessage(false, null, title, content);
//            jPushMessage.setUsername(username);
//            jPushMessage.setPushUserType(jpUserType);
//            jPushMessage.setJpSubType(jpSubType);
//            jPushMessage.setOrderNo(orderNo);
//            JPushTask.jPushMessageList.add(jPushMessage);
//            NoticeTask.messageList.add(jPushMessage);// 公众号通知、服务通知
//        }
//
//        if (ValidateUtil.isNotNull(username)) {
//
////            if (ValidateUtil.isNull(sysAppKey)) {
////                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
////            }
////            try {
////                JPushClient.getInstance().setSysCustomer(sysAppKey + "-" + jpUserType + jpSubType).setPhone(username).setTitle(title).setContent(content).pushByAlias();
////
////                //智慧商户用的时候
////                JPushClient.getInstance().setSysCustomer("o2o_jinyou-" + jpUserType + jpSubType).setPhone(sysAppKey + username).setTitle(title).setContent(content).pushByAlias();
////            } catch (Exception e) {
////                e.printStackTrace();
////            }
//
//            JPushMessage jPushMessage = new JPushMessage(false, null, title, content);
//            jPushMessage.setUsername(username);
//            jPushMessage.setPushUserType(jpUserType);
//            jPushMessage.setJpSubType(jpSubType);
//            jPushMessage.setOrderNo(orderNo);
//            JPushTask.jPushMessageList.add(jPushMessage);
//            NoticeTask.messageList.add(jPushMessage);// 公众号通知、服务通知
//        }
//
//        //是否要进行公众号推送
//        if (null != SYS_SETTINGS.IS_PUSH_WITH_GZH && 0 == 1 - SYS_SETTINGS.IS_PUSH_WITH_GZH) {
//            UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
//            if (null != userInfo && ValidateUtil.isNotNull(userInfo.getWxGzhOpenId())) {
//                SysSetting sysSetting = SysSettingMemory.getInstance().getByCode("isPushWithGzh");
//                if (null != sysSetting && ValidateUtil.isNotNull(sysSetting.getNote())) {
//                    WxGzhActionMan.sendGzhMsg(userInfo.getWxGzhOpenId(),
//                            sysSetting.getNote(),
//                            title, "",
//                            title, content);
//                }
//            }
//        }
//
//        // 服务通知
//        if (null != SYS_SETTINGS.IS_PUSH_WITH_GZH && 0 == 2 - SYS_SETTINGS.IS_PUSH_WITH_GZH) {
//            OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
//            UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
//            if (null != orderInfo && null != userInfo && ValidateUtil.isNotNull(userInfo.getWxXcxOpenId())) {
//                SysSetting sysSetting = SysSettingMemory.getInstance().getByCode("isPushWithGzh");
//                if (ValidateUtil.isNotNull(orderInfo.getPaySign())
//                        && (orderInfo.getPayType().equals("wx_xcx") || orderInfo.getPayType().equals("wx"))
//                        && null != sysSetting && ValidateUtil.isNotNull(sysSetting.getNote())) {
////                    JSONObject jsonObj = JSONObject.fromObject(orderInfo.getPaySign());
////                    if (null != jsonObj && jsonObj.containsKey("package") && ValidateUtil.isNotNull(jsonObj.getString("package"))) {
////                        String formId = jsonObj.getString("package").replace("prepay_id=", "");
////                        WXGZHAction.pushNewOrderMsg(sysSetting.getNote(), formId, userInfo.getWxXcxOpenId(), orderNo, content);
////                    }
//
//                    WXGZHAction.pushNewOrderMsg(sysSetting.getNote(), "", userInfo.getWxXcxOpenId(), orderNo, orderInfo.getShopName(), orderInfo.getShopPhone(), orderInfo.getPostmanPhone());
//
//                    /**
//                     *  处理订阅消息参数值限制
//                     *  5  商家已接单 6  商家已拒接  8  骑手已接单  19 骑手已送达 29 商家已完成
//                     */
////                    String OrderStatusName = "点击查看";
////                    if (0 == orderInfo.getOrderStatus() - ORDER_STATUS.SHOP_SURE
////                            || 0 == orderInfo.getOrderStatus() - ORDER_STATUS.SHOP_REFUSED
////                            || 0 == orderInfo.getOrderStatus() - ORDER_STATUS.POSTMAN_SURE
////                            || 0 == orderInfo.getOrderStatus() - ORDER_STATUS.POSTMAN_FINISH
////                            || 0 == orderInfo.getOrderStatus() - ORDER_STATUS.SHOP_FINISH) {
////
////                        switch (orderInfo.getOrderStatus()) {
////                            case ORDER_STATUS.SHOP_SURE: {
////                                OrderStatusName = "商家已接单";
////                                break;
////                            }
////                            case ORDER_STATUS.SHOP_REFUSED: {
////                                OrderStatusName = "商家已拒接";
////                                break;
////                            }
////                            case ORDER_STATUS.POSTMAN_SURE: {
////                                OrderStatusName = "骑手已接单";
////                                break;
////                            }
////                            case ORDER_STATUS.POSTMAN_FINISH: {
////                                OrderStatusName = "骑手已送达";
////                                break;
////                            }
////                            case ORDER_STATUS.SHOP_FINISH: {
////                                OrderStatusName = "商家已完成";
////                                break;
////                            }
////                        }
////                        WXGZHAction.pushNewOrderMsg(sysSetting.getNote(), "", userInfo.getWxXcxOpenId(), orderNo, OrderStatusName);
////                    }
//                }
//            }
//        }
    }
}
